import Vue from 'vue'
import VueRouter from 'vue-router'
import SellerPage from '@/components/statistics/page/SellerPage'
import TrendPage from '@/components/statistics/page/TrendPage'
import MapPage from '@/components/statistics/page/MapPage'
import RankPage from '@/components/statistics/page/RankPage'
import HotPage from '@/components/statistics/page/HotPage'
import StockPage from '@/components/statistics/page/StockPage'
import ScreenPage from '@/components/statistics/page/ScreenPage'

const Login = () =>
  import(/* webpackChunkName: "login_home_welcome" */ '@/views/login')
const Home = () =>
  import(/* webpackChunkName: "login_home_welcome" */ '@/views/Home')

const Amount = () =>
  import(/* webpackChunkName: "Amount" */ '@/components/amount/Amount')
const Bill = () =>
  import(/* webpackChunkName: "Bill" */ '@/components/bill/Bill')
const Fund = () =>
  import(/* webpackChunkName: "Fund" */ '@/components/fund/Fund')

const Bank = () =>
  import(/* webpackChunkName: "Bank" */ '@/components/bank/Bank')

const Income = () =>
  import(/* webpackChunkName: "Income" */ '@/components/income/Income')

const User = () =>
  import(/* webpackChunkName: "User" */ '@/components/user/User')
const Statistics = () =>
  import(
    /* webpackChunkName: "Statistics" */ '@/components/statistics/Statistics'
  )
const Travelling = () =>
  import(
    /* webpackChunkName: "Travelling" */ '@/components/statistics/Travelling'
  )

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/sellerpage',
    name: 'seller',
    component: SellerPage
  },
  {
    path: '/trendpage',
    name: 'trend',
    component: TrendPage
  },
  {
    path: '/mappage',
    name: 'map',
    component: MapPage
  },
  {
    path: '/rankpage',
    name: 'rank',
    component: RankPage
  },
  {
    path: '/hotpage',
    name: 'hot',
    component: HotPage
  },
  {
    path: '/stockpage',
    name: 'stock',
    component: StockPage
  },
  {
    path: '/statisticsHome',
    redirect: '/screen'
  },
  {
    path: '/screen',
    component: ScreenPage
  },
  {
    path: '/home',
    component: Home,
    redirect: '/statistics',
    children: [
      {
        path: '/statistics',
        component: Statistics
      },
      {
        path: '/amount',
        component: Amount
      },
      {
        path: '/bill',
        component: Bill
      },
      {
        path: '/fund',
        component: Fund
      },
      {
        path: '/bank',
        component: Bank
      },
      {
        path: '/income',
        component: Income
      },
      {
        path: '/user',
        component: User
      },
      {
        path: '/travelling',
        component: Travelling
      }
    ]
  }
]

// 避免重复路由导航错误
// 获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
// 修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes,
  mode: 'history' // 去掉url中#
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  }

  const tokenStr = window.sessionStorage.getItem('token')

  if (!tokenStr) {
    return next('/login')
  }
  next()
})

export default router
